package com.android.lovely.ts

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Build
import android.view.View
import android.widget.RemoteViews
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.android.lovely.R
import com.android.lovely.ui.activity.HomeActivity
import com.android.lovely.ui.chat.ChatMessageActivity
import com.kotlin.extensions.appContext

object LovelyMsgHelper {

    private var requestCode = 9
    private var notifyId = 486
    private const val CHANNEL_ID = "lovely_channel_id"
    private const val CHANNEL_NAME = "lovely"

    fun show(data: Map<String, String>, bitmap: Bitmap?) {
        val notificationManager = appContext.getSystemService(NotificationManager::class.java)
        addChannel(notificationManager)
        val botId: Int? = data["bot_id"]?.toInt()
        val title = data["bot_name"]?.ifEmpty { appContext.getString(R.string.app_name) } ?: appContext.getString(R.string.app_name)
        val body = data["message"].orEmpty()
        val intent = Intent(appContext, if (data.containsKey("bot_id")) ChatMessageActivity::class.java else HomeActivity::class.java).apply {
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            botId?.let { putExtra("id", it) }
        }
        val pendingIntent = pendingIntent(intent)
        val remoteViews = createRemoteView(title, body, bitmap)
        val notification = buildNotification(bitmap, pendingIntent, remoteViews)
        if (ActivityCompat.checkSelfPermission(appContext, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
            return
        }
        NotificationManagerCompat.from(appContext).notify(++notifyId, notification)
    }

    private fun pendingIntent(intent: Intent): PendingIntent? = PendingIntent.getActivity(
        appContext, ++requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )

    private fun buildNotification(bitmap: Bitmap?, pendingIntent: PendingIntent?, remoteViews: RemoteViews) =
        NotificationCompat.Builder(appContext, CHANNEL_ID).apply {
            setSmallIcon(R.mipmap.ic_launcher)
            setLargeIcon(bitmap)
            setContentIntent(pendingIntent)
            setCustomContentView(remoteViews)
            setCustomBigContentView(remoteViews)
            setAutoCancel(true)
        }.build()

    private fun createRemoteView(title: String, body: String, bitmap: Bitmap?) =
        RemoteViews(appContext.packageName, R.layout.notify_message).apply {
            setTextViewText(R.id.name, title)
            setTextViewText(R.id.content, body)
            if (bitmap == null) {
                setViewVisibility(R.id.image, View.GONE)
            } else {
                setImageViewBitmap(R.id.image, bitmap)
            }
        }

    private fun addChannel(notificationManager: NotificationManager) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
                val channel = NotificationChannel(
                    CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH
                )
                notificationManager.createNotificationChannel(channel)
            }
        }
    }


}